let express = require('express');
let crypto = require('crypto');
let router = express.Router();
let filter = require('../../components/filter');

/**
 * 查询字典数据 by p 2018-06-11
 *
 * 按条件查询字典数据
 *
 */
router.get('/', filter.authorize, function (req, res, next) {
    let params = req.query;

    var limits = "";
    var sql = "";
    var datas = [];

    req.app.db.query("select count(1) as totalCount from `dic` " + sql, datas, function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results[0].totalCount === 0) {
            res.json({"success": true, "data": {}, "totalCount": 0});
            return;
        }

        let totalCount = results[0].totalCount;

        if (params.pageNo > 0 && params.pageSize > 0) {
            limits = " limit ?, ?";
            datas.push((params.pageNo - 1) * params.pageSize);
            datas.push(params.pageSize * 1);
        }

        req.app.db.query("select * from `dic` " + limits, datas, function (err, results, fields) {
            if (err) {
                res.json({"success": false, "message": global.databaseErr, "detail": err});
                return;
            }

            res.json({"success": true, "data": results, "totalCount": totalCount, "message": ""});
        });
    });
});

/**
 * 查看字典数据 by p 2018-06-11
 *
 * 按id查看指定的字典信息
 */
router.get('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query("select * from `dic` where id = ?", [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        if (results.length === 0) {
            res.json({"success": false, "message": global.noDataErr});
            return;
        }

        res.json({"success": true, "data": results[0]});
    });
});

/**
 * 修改字典数据 by p 2018-06-11
 *
 *
 */
router.put('/:id', filter.authorize, function (req, res, next) {
    let params = req.body;

    if (params.id === undefined || params.id === '') {
        res.json({"success": false, "message": "字典编码不正确！"});
        return;
    }

    if (params.keysId === undefined || params.keysId === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.keysName === undefined || params.keysName === "") {
        res.json({"success": false, "message": "值不能为空！"});
        return;
    }

    if (params.keysValues === undefined || params.keysValues === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.name === undefined || params.name === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.values === undefined || params.values === "") {
        res.json({"success": false, "message": "值不能为空！"});
        return;
    }

    req.app.db.query("update `dic` set keysId = ?, keysName = ?, keysValues = ?, name = ?, value = ?, exception = ? where id = ?" , [params.keysId, params.keysName, params.keysValues, params.name, params.values, params.exception, params.id], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.putMsg});
    });

});

/**
 * 新增字典数据 by p 2018-06-11
 *
 *
 */
router.post('/', filter.authorize, function (req, res, next) {
    let params = req.body;

    if (params.keysId === undefined || params.keysId === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.keysName === undefined || params.keysName === "") {
        res.json({"success": false, "message": "值不能为空！"});
        return;
    }

    if (params.keysValues === undefined || params.keysValues === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.name === undefined || params.name === '') {
        res.json({"success": false, "message": "中文名称不能为空！"});
        return;
    }

    if (params.values === undefined || params.values === "") {
        res.json({"success": false, "message": "值不能为空！"});
        return;
    }

    req.app.db.query("insert into `dic`(keysId, keysName, keysValues, name, value, exception) values(?, ?, ?, ?, ?, ?)" , [params.keysId, params.keysName, params.keysValues, params.name, params.values, params.exception], function (err, results) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.postMsg});
    });
});

/**
 * 删除字典信息 by v 2018-06-11
 *
 * 按id删除指定的字典信息
 */
router.delete('/:id', filter.authorize, function (req, res, next) {
    let params = req.params;

    req.app.db.query('delete from `dic` where id = ?', [params.id], function (err, results, fields) {
        if (err) {
            res.json({"success": false, "message": global.databaseErr, "detail": err});
            return;
        }

        res.json({"success": true, "message": global.deleteMsg});
    });
});

module.exports = router;